package com.ikdong.toodle.widget;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.ikdong.toodle.R;
import com.ikdong.toodle.model.Task;
import com.ikdong.toodle.util.ComUtil;
import com.ikdong.toodle.util.Constant;
import com.ikdong.toodle.util.TaskManager;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

public class TaskListAdapter extends BaseAdapter {
	public final Long OP_MODIFY_COMP = Long.valueOf(0);
	public final Long OP_MODIFY_STAR = Long.valueOf(1);
	private LayoutInflater inflater;
	private List<Task> tasks = new ArrayList<Task>();
	private Context context;
	private TaskManager manager;
	private TaskTabAdapter tabAdapter;

	public TaskListAdapter(Context context){
		this.context = context;
		inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		manager = new TaskManager(context);
	}
	
	public void setTasks(List<Task> data){
		tasks.clear();
		tasks.addAll(data);
	}

	@Override
	public int getCount() {
		return tasks.size();
	}

	@Override
	public Task getItem(int position) {
		return tasks.get(position);
	}

	@Override
	public long getItemId(int position) {
		return tasks.get(position).getId();
	}
	
	public void setTabAdapter(TaskTabAdapter tabAdapter) {
		this.tabAdapter = tabAdapter;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView != null ? convertView : inflater.inflate(R.layout.task_list_item,parent,false);
		Task task = this.getItem(position);
		ImageButton comp = (ImageButton) view.findViewById(R.id.tli_comp);
		TextView title = (TextView) view.findViewById(R.id.tli_title);
		TextView date = (TextView) view.findViewById(R.id.tli_date);
		TextView priority = (TextView) view.findViewById(R.id.tli_priority);
		ImageButton note = (ImageButton) view.findViewById(R.id.tli_note);
		ImageButton star = (ImageButton) view.findViewById(R.id.tli_star);
		
		if(task.getCompleted()!=null && task.getCompleted()>0){ 
			comp.setImageResource(R.drawable.ic_checkbox_full);
		}else{
			comp.setImageResource(R.drawable.ic_checkbox_empty);
		}
		title.setText(task.getTitle());
		String dateStr = context.getString(R.string.lable_task_no_date);
		if(task.getDuedate()!=null && task.getDuedate()>0){
			dateStr = ComUtil.formateDate(ComUtil.parseDateByUS(task.getDuedate()), "EEE, d MMM yyyy");
		}
		date.setText(dateStr);
		priority.setText(task.getPriorityFormat());
		if(task.getNote()!=null && task.getNote().trim().length()>0){
			note.setVisibility(View.VISIBLE);
		}else{
			note.setVisibility(View.INVISIBLE);
		}
		if(Constant.TASK_STAR.equals(task.getStar())){
			star.setImageResource(R.drawable.ic_star_on);
		}else{
			star.setImageResource(R.drawable.ic_star_off);
		}
		
		comp.setOnClickListener(new ItemLister(task,OP_MODIFY_COMP));
		star.setOnClickListener(new ItemLister(task,OP_MODIFY_STAR));
		
		return view;
	}
	
	private class ItemLister implements View.OnClickListener{
		private Task task;
		private Long operation;
		
		public ItemLister(Task task,Long operation){
			this.task = task;
			this.operation = operation;
		}
		
		@Override
		public void onClick(View v) {
			if(OP_MODIFY_COMP.equals(operation)){
				task.setCompleted(task.getCompleted()==0 ? ComUtil.parseUnixStamp(new Date()):Long.valueOf(0));
				TaskListAdapter.this.notifyDataSetChanged();
				manager.updateTaskComp(task.getId(), task.getCompleted());
			}else if(OP_MODIFY_STAR.equals(operation)){
				task.setStar(task.getStar()==0 ? Long.valueOf(1):Long.valueOf(0));
				TaskListAdapter.this.notifyDataSetChanged();
				manager.updateTaskStar(task.getId(), task.getStar());
			}
			
			if(tabAdapter!=null){
				tabAdapter.allDataChanged();
			}
		}
	}
	
	public void removeItem(long id){
		for(int i=0; i<tasks.size(); i++){
			if(tasks.get(i).getId()==id){
				tasks.remove(i);
				this.notifyDataSetChanged();
				Log.d(Constant.TAG, "removeItem(): "+id);
				return;
			}
		}
	}

}
